package handler

import (
	"context"
	"encoding/json"
	"gitee.com/DonHz/electronic-business-systems/mxshop_srvs/goods_srv/global"
	"gitee.com/DonHz/electronic-business-systems/mxshop_srvs/goods_srv/model"
	"gitee.com/DonHz/electronic-business-systems/mxshop_srvs/goods_srv/proto"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"google.golang.org/protobuf/types/known/emptypb"
)

// // 商品分类
func (s *GoodsServer) GetAllCategorysList(context.Context, *emptypb.Empty) (*proto.CategoryListResponse, error) {
	var categorys []model.Category
	//var resp []*proto.CategoryInfoResponse
	global.DB.Where(&model.Category{Level: 1}).Preload("SubCategory.SubCategory").Find(&categorys)
	//for _, category := range categorys {
	//	resp = append(resp, &proto.CategoryInfoResponse{
	//		Id:             int64(category.ID),
	//		Name:           category.Name,
	//		ParentCategory: int64(category.ParentCategoryID),
	//		Level:          int64(category.Level),
	//		IsTab:          category.IsTab,
	//	})
	//}
	b, _ := json.Marshal(&categorys)
	return &proto.CategoryListResponse{JsonData: string(b)}, nil
}

// 获取子分类
func (s *GoodsServer) GetSubCategory(ctx context.Context, req *proto.CategoryListRequest) (*proto.SubCategoryListResponse, error) {
	var resp proto.SubCategoryListResponse
	var category model.Category
	if res := global.DB.First(&category, req.Id); res.RowsAffected == 0 {
		return nil, status.Error(codes.NotFound, "商品分类不存在")
	}
	resp.Info = &proto.CategoryInfoResponse{
		Id:             int64(category.ID),
		Name:           category.Name,
		Level:          int64(category.Level),
		IsTab:          category.IsTab,
		ParentCategory: int64(category.ParentCategoryID),
	}
	preloads := "SubCategory"
	if category.Level == 1 {
		preloads = "SubCategory.SubCategory"
	}
	var subCategorys []model.Category
	global.DB.Where(&model.Category{ParentCategoryID: int32(req.Id)}).Preload(preloads).Find(&subCategorys)
	var subCategoryResponse []*proto.CategoryInfoResponse
	for _, subCategory := range subCategorys {
		subCategoryResponse = append(subCategoryResponse, &proto.CategoryInfoResponse{
			Id:             int64(subCategory.ID),
			Name:           subCategory.Name,
			Level:          int64(subCategory.Level),
			IsTab:          subCategory.IsTab,
			ParentCategory: int64(subCategory.ParentCategoryID),
		})
	}
	resp.SubCategorys = subCategoryResponse
	return &resp, nil
}

func (s *GoodsServer) CreateCategory(ctx context.Context, req *proto.CategoryInfoRequest) (*proto.CategoryInfoResponse, error) {
	var category model.Category
	if result := global.DB.First(&category, req.Id); result.RowsAffected == 0 {
		return nil, status.Errorf(codes.NotFound, "商品分类不存在")
	}
	category.Name = req.Name
	category.Level = int32((req.Level))
	if req.Level != 1 {
		category.ParentCategoryID = int32(req.ParentCategory)
	}

	category.IsTab = req.IsTab
	global.DB.Save(&category)
	return &proto.CategoryInfoResponse{Id: int64(category.ID)}, nil
}

func (s *GoodsServer) DeleteCategory(ctx context.Context, req *proto.DeleteCategoryRequest) (*emptypb.Empty, error) {
	if result := global.DB.Delete(&model.Category{}, req.Id); result.RowsAffected == 0 {
		return nil, status.Errorf(codes.NotFound, "品牌分类不存在")
	}

	return &emptypb.Empty{}, nil
}

func (s *GoodsServer) UpdateCategory(ctx context.Context, req *proto.CategoryInfoRequest) (*emptypb.Empty, error) {
	var category model.Category
	if result := global.DB.First(&category, req.Id); result.RowsAffected == 0 {
		return nil, status.Errorf(codes.NotFound, "商品分类不存在")
	}

	if req.Name != "" {
		category.Name = req.Name
	}
	if req.ParentCategory != 0 {
		category.ParentCategoryID = int32(req.ParentCategory)
	}
	if req.Level != 0 {
		category.Level = int32(req.Level)
	}
	if req.IsTab {
		category.IsTab = req.IsTab
	}

	global.DB.Save(&category)
	return &emptypb.Empty{}, nil
}
